using System;
using System.ServiceModel;
namespace Net35.Xrm.Sdk.Client
{
	internal static class ChannelExtensions
	{
		public static void Abort(this System.ServiceModel.ICommunicationObject communicationObject)
		{
			if (communicationObject == null)
			{
				return;
			}
			communicationObject.Abort();
		}
		public static void Close(this System.ServiceModel.ICommunicationObject communicationObject, bool throwOnException = true)
		{
			if (communicationObject == null)
			{
				return;
			}
			try
			{
				System.ServiceModel.CommunicationState state = communicationObject.State;
				if (System.ServiceModel.CommunicationState.Faulted == state)
				{
					communicationObject.Abort();
				}
				else if (state != System.ServiceModel.CommunicationState.Closing && state != System.ServiceModel.CommunicationState.Closed)
				{
					communicationObject.Close();
				}
			}
			catch (System.ServiceModel.CommunicationException)
			{
				communicationObject.Abort();
			}
			catch (TimeoutException)
			{
				communicationObject.Abort();
			}
			catch (Exception)
			{
				communicationObject.Abort();
				if (throwOnException)
				{
					throw;
				}
			}
		}
	}
}
